package models

import (
	"fmt"
	"strconv"
	"time"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	//_ "github.com/mattn/go-sqlite3"
	_ "github.com/go-sql-driver/mysql"
)

const (
	DB_NAME         = "blog.db"
	_SQLITE3_DRIVER = "sqlite3"
)

type Fenlei struct {
	Id          int64
	Title       string
	Views       int64     `orm:"index"`
	CreatedTime time.Time `orm:"index"`
	TopicCount  int64
	TuBiao      string
	IsAddnav    bool
}

type Topic struct {
	Id           int64
	Uid          int64
	Title        string
	Content_md   string    `orm:"type(text)"`
	Content_html string    `orm:"type(text)"`
	Attachment   string    //附件，可以上传图片
	CreatedTime  time.Time `orm:"index"`
	Views        int64     `orm:"index"`
	UpdatedTime  time.Time
	Author       string
	ReplyTime    time.Time
	ReplyCount   int64
}

func RegisterDB() {
	orm.RegisterModel(new(Fenlei), new(Topic))
	orm.RegisterDriver("mysql", orm.DRMySQL)
	orm.RegisterDataBase("default", "mysql", "root:517517@/blog?charset=utf8", 10)
}
func AddTopic(fenleiNumber, title, content_md, content_html string) error {
	o := orm.NewOrm()
	UidNumber, err := strconv.Atoi(fenleiNumber)
	if err != nil {
		beego.Error("string fenleiNumber to int Uidnumber error!")
	}
	topic := &Topic{
		Title:        title,
		Uid:          int64(UidNumber),
		Content_md:   content_md,
		Content_html: content_html,
		CreatedTime:  time.Now(),
		Views:        0,
		UpdatedTime:  time.Now(),
		ReplyTime:    time.Now(),
	}

	_, err = o.Insert(topic)
	return err
}

func GetTopics() ([]*Topic, error) {
	o := orm.NewOrm()
	topics := make([]*Topic, 0)
	qs := o.QueryTable("topic")
	_, err := qs.All(&topics)
	return topics, err
}

func GetTopic(Id string) (*Topic, error) {
	tid, _ := strconv.ParseInt(Id, 10, 64)
	topic := &Topic{
		Id: tid,
	}
	o := orm.NewOrm()
	err := o.Read(topic)
	if err != nil {
		return nil, err
	}
	return topic, nil
}
func AddFenlei(tubiao, name string) error {
	o := orm.NewOrm()

	fenlei := &Fenlei{Title: name, TuBiao: tubiao, CreatedTime: time.Now()}

	qs := o.QueryTable("fenlei")
	err := qs.Filter("title", name).One(fenlei)
	if err == nil {
		return err
	}
	_, err = o.Insert(fenlei)
	return err
}

func GetFenleis() ([]*Fenlei, error) {
	o := orm.NewOrm()

	cates := make([]*Fenlei, 0)

	qs := o.QueryTable("fenlei")
	_, err := qs.All(&cates)
	return cates, err
}
func UpdateTopic(id, title, content_md, content_html string) error {
	fmt.Printf("id = %s\n", id)
	o := orm.NewOrm()
	num, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		beego.Error(err)
		return err
	}
	qs := o.QueryTable("topic")
	qs.Filter("Id", num).Update(
		orm.Params{
			"Title":        title,
			"Content_md":   content_md,
			"Content_html": content_html,
			"UpdatedTime":  time.Now(),
		})
	return err
}
func UpdateFenlei(id string, status bool) error {

	o := orm.NewOrm()
	num, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		beego.Error(err)
		return err
	}
	_, err = o.QueryTable("Fenlei").Filter("Id", num).Update(
		orm.Params{
			"IsAddnav": status,
		})
	return err
}

func DeleteTopic(id string) error {
	num, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		return err
	}
	o := orm.NewOrm()
	if _, err := o.Delete(&Topic{Id: num}); err != nil {
		return err
	}
	return nil
}
